package com.bookstore.web;

import java.util.List;

/**
 * Created by Admin on 2016/8/11.
 */
public class Page<T> {
    /**
     * 当前页的页码
     */
    private int pageNo;
    /**
     * 本页面需要显示的List
     */
    private List<T> list;
    /**
     * 每页多少条记录
     */
    private int pageSize = 3;



    /**
     * 总记录数(book 的id ):可以用来计算总页数
     */
    private long totalItemNumber;

    public long getTotalItemNumber() {
        return totalItemNumber;
    }
    public Page(int pageNo) {
        super();
        this.pageNo = pageNo;
    }

    /**
     * 返回当前页的页码,需要通过getTotalPageNumber() 进行校验
     *
     * @return
     */
    public int getPageNo() {
        if (pageNo < 0) {
            pageNo = 1;
        }
        if (pageNo > getTotalPageNumber()) {
            pageNo = (int) getTotalPageNumber();
        }
        return pageNo;
    }

    public int getPageSize() {
        return pageSize;
    }


    /**
     * 获取当前页需要显示的List
     *
     * @return
     */
    public List<T> getList() {
        return list;
    }

    /**
     * 获取总页数,需要通过totalItemNumber/pageSize 进行计算后返回
     *
     * @return
     */
    public long getTotalPageNumber() {
        int totalPageNumber = (int) (totalItemNumber / pageSize);
        if (totalItemNumber % pageSize != 0) {
            totalPageNumber++;
        }
        return totalPageNumber;
    }

    /**
     * 设置当前页需要显示的List 对象
     *
     * @return
     */
    public void setList(List<T> list) {
        this.list = list;
    }

    @Override
    public String toString() {
        return "Page{" +
                "pageNo=" + pageNo +
                ", list=" + list +
                ", pageSize=" + pageSize +
                ", totalItemNumber=" + totalItemNumber +
                '}';
    }

    /**
     * 设置总记录数
     *
     * @param totalItemNumber
     */
    public void setTotalItemNumber(long totalItemNumber) {
        this.totalItemNumber = totalItemNumber;
    }

    public boolean isHasNext() {
        if (getPageNo() < getTotalPageNumber()) {
            return true;
        }
        return false;
    }

    public boolean isHasPrev() {
        if (getPageNo() > 1) {
            return true;
        }
        return false;
    }

    public int getPrevPage() {
        if (isHasPrev()) {
            return getPageNo() - 1;
        }
        return getPageNo();
    }

    public int getNextPage() {
        if (isHasNext()) {
            return getPageNo() + 1;
        }
        return getPageNo();
    }
}
